/**
 * 
 */
package com.wsd.service.impl;

import java.sql.Timestamp;
import java.util.List;

import com.wsd.bean.ProductComment;
import com.wsd.bean.ProductRank;
import com.wsd.bean.Products;
import com.wsd.dao.ProductCommentDaoInterface;
import com.wsd.dao.ProductRankDaoInterface;
import com.wsd.dao.ProductsDaoInterface;
import com.wsd.service.ProductService;

/**
 * @author TuZiJun
 *
 */
public class ProductServiceImpl implements ProductService {
	private ProductsDaoInterface productsDAO = null;
	private ProductRankDaoInterface productRankDAO;
	private ProductCommentDaoInterface productCommentDAO = null;
	/**
	 * @param productCommentDAO the productCommentDAO to set
	 */
	public void setProductCommentDAO(ProductCommentDaoInterface productCommentDAO) {
		this.productCommentDAO = productCommentDAO;
	}

	public void setProductsDAO(ProductsDaoInterface productsDAO) {
		this.productsDAO = productsDAO;
	}
	
	public ProductRankDaoInterface getProductRankDAO() {
		return productRankDAO;
	}

	public void setProductRankDAO(ProductRankDaoInterface productRankDAO) {
		this.productRankDAO = productRankDAO;
	}

	public ProductsDaoInterface getProductsDAO() {
		return productsDAO;
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#categoryProducts(int, int, int)
	 */
	public List<Products> categoryProducts(int page, int pageSize, int cid) {
		int start = (page - 1) * pageSize;
		return productsDAO.categoryProducts(start, pageSize, cid);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findById(int)
	 */
	public Products findById(int pid) {
		return productsDAO.findById(pid);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findOnePage(int, int)
	 */
	public List<Products> findOnePage(int page, int pageSize) {
		int start = (page - 1) * pageSize;
		return productsDAO.findOnePage(start, pageSize);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#getTotallines(java.lang.String)
	 */
	public int getTotalPages(int pageSize, String keyWord) {
		int totalPage = ((Long) productsDAO.getTotallines(keyWord).get(0)).intValue();
		totalPage = totalPage % pageSize == 0 ? totalPage / pageSize : totalPage / pageSize + 1;
		return totalPage;
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#getTotallines()
	 */
	public int getTotalPages(int pageSize) {
		int totalPage = ((Long) productsDAO.getTotallines().get(0)).intValue();
		totalPage = totalPage % pageSize == 0 ? totalPage / pageSize : totalPage / pageSize + 1;
		return totalPage;
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#searchProducts(int, int, java.lang.String)
	 */
	public List<Products> searchProducts(int page, int pageSize, String keyWord) {
		int start = (page - 1) * pageSize;
		return productsDAO.searchProducts(start, pageSize, keyWord);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#getTotalPages(int, int, int)
	 */
	public int getTotalPages(int pageSize, int cid) {
		int totalPage = ((Long) productsDAO.getTotallines(cid).get(0)).intValue();
		totalPage = totalPage % pageSize == 0 ? totalPage / pageSize : totalPage / pageSize + 1;
		return totalPage;
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#save(com.wsd.bean.Products)
	 */
	public void save(Products product) {
		productsDAO.save(product);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#getProductCount(int)
	 */
	public int getProductCount(int cid) {
		Long l = (Long)productsDAO.getTotallines(cid).get(0);
		return  l.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#deleteProduct(com.wsd.bean.Products)
	 */
	public void deleteProduct(int[] ids) {
		for(int id : ids){
			productsDAO.delete(findById(id));
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#update(com.wsd.bean.Products)
	 */
	public void update(Products product) {
		productsDAO.update(product);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#deletePrice(com.wsd.bean.Products)
	 */
	public void deletePrice(Products preProduct) {
		productsDAO.deletePrice(preProduct);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#saveRank(int[], java.lang.Short)
	 */
	public void saveRank(int[] ids, Short model) {
		for(int id : ids){
			if(!productRankDAO.checkExist(id,model)){
				ProductRank pr = new ProductRank();
				Products product = new Products();
				product.setPId(id);
				pr.setBModel(model);
				pr.setBName("");
				pr.setProducts(product);
				productRankDAO.save(pr);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#rankProducts(java.lang.Short, int, int)
	 */
	public List<Products> rankProducts(Short model, int start, int limit) {
		
		return productRankDAO.rankProducts(model,start,limit);
	}

	public int rankProductsCount(Short model) {
		
		return productRankDAO.rankProductsCount(model);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#removeRank(int[], java.lang.Short)
	 */
	public void deleteRank(int[] ids, Short model) {
		for(int id : ids){
			productRankDAO.removeRank(id,model);
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#rankByModel(short)
	 */
	public List<Products> rankByModel(short model, int num) {
		return productsDAO.rankByModel(model, num);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#saveComment(com.wsd.bean.ProductComment)
	 */
	public void saveComment(ProductComment comment) {
		comment.setPCTime(new Timestamp(System.currentTimeMillis()));
		comment.setPCState(false);
		productCommentDAO.save(comment);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findByComment(com.wsd.bean.ProductComment, int, int)
	 */
	public List<ProductComment> findByComment(ProductComment comment, int page,
			int pageSize) {
		int start = (page - 1) * pageSize;
		return productCommentDAO.findByExample(comment, start, pageSize);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findByCommentCount(com.wsd.bean.ProductComment)
	 */
	public int findByCommentCount(ProductComment comment, int pageSize) {
		int totalPage = productCommentDAO.findByExampleCount(comment);System.out.println(totalPage);
		totalPage = totalPage % pageSize == 0 ? totalPage / pageSize : totalPage / pageSize + 1;
		return totalPage;
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#deleteComment(int)
	 */
	public void deleteComment(int pid) {
		productCommentDAO.deleteById(pid);
		
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findAllComments(int, int)
	 */
	public List<ProductComment> findAllComments(int start, int limit) {
		return productCommentDAO.findAll(start,limit);
	}

	/* (non-Javadoc)
	 * @see com.wsd.service.ProductService#findAllCommentsCount()
	 */
	public int findAllCommentsCount() {
		return productCommentDAO.findAllCount();
	}

}
